package com.yd;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by john on 16/3/10.
 */
public class Game {

    private Player master;

    private GameRules gameRules;

    private int cardIndex = 0;

    private List<Card> cards;

    private List<Player> players = new ArrayList<Player>();

    private Scanner in;

    private String[] results = {
            "",
            "爆表",
            "十点半",
            "五龙"
    };

    public Game(Player master) {
        if (master == null) {
            throw new IllegalArgumentException("必须要有庄家");
        }
        this.master = master;
        gameRules = new GameRulesImpl();
        initCards();
    }

    private void initCards() {
        cards = new ArrayList<>(52);
        // mock data
        cards.add(CardFactory.buildCard("8", 8f));
        cards.add(CardFactory.buildCard("8", 8f));
        cards.add(CardFactory.buildCard("K", 0.5f));
        cards.add(CardFactory.buildCard("3", 3f));
        cards.add(CardFactory.buildCard("5", 5f));
        cards.add(CardFactory.buildCard("3", 3f));
        cards.add(CardFactory.buildCard("5", 5f));
        cards.add(CardFactory.buildCard("2", 2f));
        cards.add(CardFactory.buildCard("7", 7f));
        cards.add(CardFactory.buildCard("7", 7f));
        cards.add(CardFactory.buildCard("7", 7f));
        cards.add(CardFactory.buildCard("7", 7f));
    }

    private Card getCard() {
        return cards.get(cardIndex++);
    }

    public void addPlayer(Player player) {
        if (player == null) {
            throw new IllegalArgumentException("玩家不能为空");
        }
        players.add(player);
    }

    /**
     * 游戏开始
     */
    public void start() {
        if (players.size() == 0) {
            throw new IllegalStateException("必须要有玩家才能开始");
        }
        in = new Scanner(System.in);
        executeBet();
        executeGiveCard();
        executeTakeCard();
        over();
        in.close();
    }

    protected void executeBet() {
        System.out.println("=========== 1.游戏开始, 请玩家下注 =============");
        players.forEach(player -> {
            System.out.print(player.getName() + "下注: ");
            int bet = in.nextInt();
            player.bet(bet);
        });
    }

    protected void executeGiveCard() {
        System.out.println("=========== 2.开始发牌 =============");

        // 玩家先发牌
        players.forEach(player -> givePlayerCard(player));

        // 给庄家发牌
        givePlayerCard(master);
    }

    protected void executeTakeCard() {
        System.out.println("=========== 3.开始要牌, 按任意键回车要牌, 输入p回车则不要牌 =============");
        players.forEach(player -> {
            takeCard(player);
        });

        takeCard(master);
    }

    /**
     * 发牌
     * @param player
     */
    private void givePlayerCard(Player player) {
        Card card = getCard();
        player.addCard(card);
        System.out.println(player.getName() + "得到一张牌, 点数为: " + card.getCount() + "点, 共" + player.getCount() + "点");
    }

    /**
     * 要牌
     * @param player
     */
    private void takeCard(Player player) {
        boolean pass = false;
        while (!pass) {
            System.out.print(player.getName() + "是否要牌? ");
            String input = in.next();
            pass = "p".equalsIgnoreCase(input);
            if (!pass) {
                givePlayerCard(player);
                if (player.getStatus() != Player.STATUS_NORMAL) {
                    pass = true;
                    System.out.println(player.getName() + results[player.getStatus()]);
                }
            }
        }
    }

    /**
     * 结束打印结果
     */
    private void over() {
        System.out.println("=========== 4.Game Over =============");
        players.forEach(player -> {
            String masterName = master.getName();
            String playerName = player.getName();
            String winner = gameRules.compare(master, player) ? masterName : playerName;
            System.out.println(masterName + "对" + playerName + ": " + winner + "赢");
        });
    }

}
